﻿//-------------------------------------------------------------------
//版权所有：版权所有(C) 2010，Microsoft(China) Co.,LTD
//系统名称： 
//文件名称：EmailUtility
//模块名称：邮件服务
//模块编号：
//作　　者：lhc
//完成日期：2010-1-25
//功能说明：
//-----------------------------------------------------------------
//修改记录： 
//修改人：   
//修改时间： 
//修改内容： 
//-----------------------------------------------------------------

using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Web;

namespace WFramework.Email
{
    /// <summary>
    /// 邮件服务 
    /// </summary>
    public class EmailUtility : IEmailUtility
    {


        /// <summary>
        /// 发送邮件 如果不配置
        /// FromEmailAddress(发送邮件账户)  不配置读取Web.config的<add key="SMTPUser" />
        /// FromEmailPassword(发送邮件密码) 不配置读取Web.config的<add key="SMTPPwd" />
        /// EmailHostName(邮件服务器)       不配置读取Web.config的<add key="SMTPServer" />
        /// </summary>
        /// <param name="mailInfo">邮件配置实体</param>
        public void Send(MailInfo mailInfo)
        { 
            if (string.IsNullOrEmpty(mailInfo.FromEmailAddress))
            {
                mailInfo.FromEmailAddress = ConfigurationManager.AppSettings["SMTPUser"];
            }
            if (string.IsNullOrEmpty(mailInfo.FromEmailPassword))
            {
                mailInfo.FromEmailPassword = ConfigurationManager.AppSettings["SMTPPwd"];
            }
            if (string.IsNullOrEmpty(mailInfo.EmailHostName))
            {
                mailInfo.EmailHostName = ConfigurationManager.AppSettings["SMTPServer"];
            }
            if (mailInfo.EmailPort <= 0)
            {
                mailInfo.EmailPort = int.Parse(ConfigurationManager.AppSettings["SMTPPort"]);
            }
            Send(mailInfo.ToEmailAddress, mailInfo.ToCcEmailAddress, mailInfo.AttachmentList, mailInfo.FromEmailAddress, mailInfo.FromEmailPassword, mailInfo.EmailPersonName, mailInfo.EmailSubject, mailInfo.EmailBody, mailInfo.IsBodyHtml, mailInfo.EmailPriority, mailInfo.EmailPort, mailInfo.EmailHostName, mailInfo.IsEnableSsl, mailInfo.EncodingType);
            
        }


        /// <summary>
        /// 邮件发送
        /// </summary>
        /// <param name="toEmailAddress">发送目标邮箱列表</param>
        /// <param name="toCcEmailAddress"></param>
        /// <param name="attachmentList">电子邮件附件列表</param>
        /// <param name="fromEmailAddress">发送账户</param>
        /// <param name="fromEmailPassword">发送密码</param>
        /// <param name="emailPersonName">发件人名</param>
        /// <param name="emailSubject">邮件标题</param>
        /// <param name="emailBody">邮件内容</param>
        /// <param name="isBodyHtml">是否HTML内容 默认为是</param>
        /// <param name="emailPriority">邮件优先级</param>
        /// <param name="port">邮箱端口号</param>
        /// <param name="emailHostName">邮箱服务器地址</param>
        /// <param name="isEnableSsl">邮件是否加密:true(加密),false(不加密)  默认为true</param>
        /// <param name="encodingType">编码格式</param>
        public void Send(IList<string> toEmailAddress, IList<string> toCcEmailAddress, IList<Attachment> attachmentList, string fromEmailAddress, string fromEmailPassword, string emailPersonName, string emailSubject, string emailBody, bool isBodyHtml, EmailPriorityEnum emailPriority, int port, string emailHostName, bool isEnableSsl, Encoding encodingType)
        {
             
            var mails = new MailMessage();
            //编码类型
            Encoding emaiEncodingType = encodingType;

            //添加发送地址
            foreach (string to in toEmailAddress)
            {
                mails.To.Add(to);
            }
            // 添加抄送地址
            foreach (string to in toCcEmailAddress)
            {
                mails.CC.Add(to);
            }
            //添加附件  
            foreach (Attachment i in attachmentList)
            {
                mails.Attachments.Add(i);
            }

            mails.From = new MailAddress(fromEmailAddress, emailPersonName, emaiEncodingType);
            mails.Subject = emailSubject;
            mails.SubjectEncoding = emaiEncodingType;
            mails.Body = HttpUtility.HtmlDecode(emailBody);
            mails.BodyEncoding = emaiEncodingType;
            //设置邮件是否为HTML格式
            mails.IsBodyHtml = isBodyHtml != false;
            //设置邮件优级先级
            switch (emailPriority)
            {
                case EmailPriorityEnum.Normal:
                    mails.Priority = MailPriority.Normal;
                    break;
                case EmailPriorityEnum.Low:
                    mails.Priority = MailPriority.Low;
                    break;
                default:
                    mails.Priority = MailPriority.High;
                    break;
            }
            var client = new SmtpClient
            {
                Credentials = new NetworkCredential(fromEmailAddress, fromEmailPassword),
                Port = port,
                Host = emailHostName,
                EnableSsl = isEnableSsl
            };


            client.Send(mails);
             
        }


    }

    /// <summary>
    /// MailInfo实体
    /// </summary>
    public sealed class MailInfo
    {
       
        /// <summary>
        /// 收件人邮箱地址
        /// </summary>
        public IList<string> ToEmailAddress = new List<string>();

        /// <summary>
        /// 抄送人邮箱地址
        /// </summary>
        public IList<string> ToCcEmailAddress = new List<string>();

        /// <summary>
        /// 发件人邮箱地址（可选）
        /// </summary>
        public string FromEmailAddress = string.Empty;

        /// <summary>
        /// 发件人邮箱密码(可选)
        /// </summary>
        public string FromEmailPassword = string.Empty;

        /// <summary>
        /// 邮件主题
        /// </summary>
        public string EmailSubject;

        /// <summary>
        /// 邮件内容
        /// </summary>
        public string EmailBody;

        /// <summary>
        /// 发件人姓名
        /// </summary>
        public string EmailPersonName;

        /// <summary>
        /// SMTP主机名称(可选)
        /// 例:Gmail为smtp.gmail.com
        /// </summary>
        public string EmailHostName;

        /// <summary>
        /// 邮件优先级：high（高）、low(低)、normal(正常)(可选)
        /// 默认为high
        /// </summary>
        public EmailPriorityEnum EmailPriority = EmailPriorityEnum.Normal;

        /// <summary>
        /// 邮箱端口号
        /// 例:Gmail为587,一般为25(可选)
        /// </summary>
        public int EmailPort;

        /// <summary>
        /// 邮件是否加密:true(加密),false(不加密)(可选)
        /// 默认为true
        /// </summary>
        public bool IsEnableSsl = false;

        /// <summary>
        /// 邮件内容是否为HTML格式(true加密,false不加密),默认为false(可选)
        /// </summary>
        public bool IsBodyHtml = true;

        /// <summary>
        /// 邮件编码类型:UTF7、UTF8(推荐,默认)、UTF32、ASCII和Default.(可选)
        /// </summary>
        public Encoding EncodingType = Encoding.UTF8;

        /// <summary>
        /// 附件(可选)
        /// </summary>
        public IList<Attachment> AttachmentList = new List<Attachment>();
         
    }



    /// <summary>
    ///  邮件优先级：high（高）、low(低)、normal(正常)
    /// </summary>
    public enum EmailPriorityEnum
    {
         
        /// <summary>
        /// 高
        /// </summary>
        [Description("高")]
        High,
        /// <summary>
        /// 低
        /// </summary>
        [Description("低")]
        Low,
        /// <summary>
        /// 正常
        /// </summary>
        [Description("正常")]
        Normal 
    }




}
